<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Redis的两种持久化机制 | Reality</title><meta name="keywords" content="Redis,面试"><meta name="author" content="dtyy"><meta name="copyright" content="dtyy"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Redis提供了了不同级别的持久化方式RDB和AOF。 1 RDB方式–Redis DataBase1.1 什么是RDBRDB：每隔一段时间，把内存中的数据写入磁盘的临时文件，作为快照，恢复的时候把快照文件读进内存。如果宕机重启，内存数据丢失，再次启动redis后，则会恢复。 1.2 RDB自动触发配置RDB默认开启。 ## SNAPSHOTTING  # save m n表示m秒内数据集存在n次">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis的两种持久化机制">
<meta property="og:url" content="https://dtyytop.gitee.io/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-chi-jiu-hua-ji-zhi/index.html">
<meta property="og:site_name" content="Reality">
<meta property="og:description" content="Redis提供了了不同级别的持久化方式RDB和AOF。 1 RDB方式–Redis DataBase1.1 什么是RDBRDB：每隔一段时间，把内存中的数据写入磁盘的临时文件，作为快照，恢复的时候把快照文件读进内存。如果宕机重启，内存数据丢失，再次启动redis后，则会恢复。 1.2 RDB自动触发配置RDB默认开启。 ## SNAPSHOTTING  # save m n表示m秒内数据集存在n次">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222634.jpg">
<meta property="article:published_time" content="2021-05-29T16:00:00.000Z">
<meta property="article:modified_time" content="2021-06-07T16:19:09.562Z">
<meta property="article:author" content="dtyy">
<meta property="article:tag" content="Redis">
<meta property="article:tag" content="面试">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222634.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://dtyytop.gitee.io/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-chi-jiu-hua-ji-zhi/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"prismjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: true
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: dtyy","link":"链接: ","source":"来源: Reality","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Redis的两种持久化机制',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-06-08 00:19:09'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="Reality" type="application/atom+xml">

<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">95</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/gallery/"><i class="fa-fw fas fa-images"></i><span> 图片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222634.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Reality</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/gallery/"><i class="fa-fw fas fa-images"></i><span> 图片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Redis的两种持久化机制</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-05-29T16:00:00.000Z" title="发表于 2021-05-30 00:00:00">2021-05-30</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-06-07T16:19:09.562Z" title="更新于 2021-06-08 00:19:09">2021-06-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Redis/">Redis</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>7分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Redis的两种持久化机制"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>Redis提供了了不同级别的持久化方式RDB和AOF。</p>
<h2 id="1-RDB方式–Redis-DataBase"><a href="#1-RDB方式–Redis-DataBase" class="headerlink" title="1 RDB方式–Redis DataBase"></a>1 RDB方式–Redis DataBase</h2><h3 id="1-1-什么是RDB"><a href="#1-1-什么是RDB" class="headerlink" title="1.1 什么是RDB"></a>1.1 什么是RDB</h3><p>RDB：每隔一段时间，把内存中的数据写入磁盘的临时文件，作为快照，恢复的时候把快照文件读进内存。如果宕机重启，内存数据丢失，再次启动redis后，则会恢复。</p>
<h3 id="1-2-RDB自动触发配置"><a href="#1-2-RDB自动触发配置" class="headerlink" title="1.2 RDB自动触发配置"></a>1.2 RDB自动触发配置</h3><p>RDB默认开启。</p>
<pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">## SNAPSHOTTING 
# save m n表示m秒内数据集存在n次修改，自动触发bgsave
save 900 1 
save 300 10 
save 60 10000 

# yes：如果save过程出错，则停止写操作
# no：可能造成数据不一致
stop-writes-on-bgsave-error yes

# yes：开启rdb压缩模式
# no：关闭，会节约cpu损耗，但是文件会大，道理同nginx
rdbcompression yes

# yes：使用CRC64算法校验对rdb进行数据校验，有10%性能损耗
# no：不校验
rdbchecksum yes

# db文件名
dbfilename "dump.rdb"

# 目录
dir "/usr/local/redis/working"<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="1-3-RDB优劣势"><a href="#1-3-RDB优劣势" class="headerlink" title="1.3 RDB优劣势"></a>1.3 RDB优劣势</h3><ul>
<li>优势<ol>
<li>每隔一段时间备份，全量备份【数据冷备】</li>
<li>灾备简单，可以远程传输【复制传输】</li>
<li>保存RDB文件时，主线程唯一的工作是fork出一个子进程。子进程备份的时候，主进程不会有任何io操作（不会有写入修改或删除），保证备份数据的的完整性，最大化Redis性能。</li>
<li>相对AOF来说，当有更大文件的时候RDB重启恢复更快。</li>
</ol>
</li>
<li>劣势<ol>
<li>发生故障时，有可能会丢失最后一次的备份数据</li>
<li>无法做到实时或者秒级持久化。内部的bgsave每次运行fork创建子进程，如果数据集较大，fork非常耗时，频繁执行成本很高。</li>
<li>RDB文件是特定二进制格式保存，存在老版本Redis无法兼容新版RDB格式的问题。</li>
</ol>
</li>
</ul>
<h3 id="1-4-RDB运作流程"><a href="#1-4-RDB运作流程" class="headerlink" title="1.4 RDB运作流程"></a>1.4 RDB运作流程</h3><p>RDB主要是bgsave操作。save命令是同步操作，会阻塞Redis服务器，已经废弃；bgsave命令是异步操作，Redis主进程fork操作创建子进程，持久化过程有子进程完成。fork操作过程中主进程阻塞。</p>
<img src="https://gitee.com/dtyytop/blogimage/raw/master/img/20210530234144.png" alt="image-20210406215450350" style="zoom: 67%;">

<p>RDB的触发操作有手动bgsave、主从复制、shutdown、debug reload。</p>
<h2 id="2-AOF方式–Append-Only-File"><a href="#2-AOF方式–Append-Only-File" class="headerlink" title="2 AOF方式–Append Only File"></a>2 AOF方式–Append Only File</h2><p>AOF以日志的形式记录用户请求的【写】操作，文件是以追加的形式，Redis的AOF恢复其实就是将AOF文件从开始到结尾读取执行写操作。有些类似MySQL的binlog。</p>
<p>AOF的主要作用是解决数据持久化的实时性。</p>
<h3 id="2-1-AOF的配置"><a href="#2-1-AOF的配置" class="headerlink" title="2.1 AOF的配置"></a>2.1 AOF的配置</h3><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">## APPEND ONLY MODE
# AOF默认关闭 yes开启
appendonly no

# 指令日志文件名
appendfilename "appendonly.aof"

# 同步策略
# appendfsync always 每次写入备份，安全数据完整，性能差
appendfsync everysec 每秒备份，推荐
# appendfsync no

# 重写的时候是否要同步 
no-appendfsync-on-rewrite no

# 重写机制：避免文件越来越大，自动优化压缩指令，会fork一个新的进程去完成重写动作，新进程里的内存数据会被重写，此时旧的aof文件不会被读取使用，类似rdb
# Redis会记录上一次重写的大小（或者启动初始值）。当前AOF文件的大小是上次AOF大小的100% 并且文件体积达到64m，满足两者则触发重写
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

# redis在恢复时，会忽略最后一条可能存在问题的指令。默认值yes
aof-load-truncated yes

# 重启redis恢复数据时，使用混合持久化。 redis5默认yes
aof-use-rdb-preamble yes<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>



<h3 id="2-2-AOF运作流程"><a href="#2-2-AOF运作流程" class="headerlink" title="2.2 AOF运作流程"></a>2.2 AOF运作流程</h3><h4 id="AOF的工作流程："><a href="#AOF的工作流程：" class="headerlink" title="AOF的工作流程："></a>AOF的工作流程：</h4><ol>
<li>当命令写入会追加到aof_buf缓冲区；</li>
<li>AOF缓冲区根据配置策略向硬盘做同步操作；</li>
<li>AOF足够大时，触发对AOF文件的重写；</li>
<li>Redis服务器重启时，加载AOF文件进行数据恢复。</li>
</ol>
<p>Redis使用单线程响应命令，如果直接追加到硬盘，影响性能。同时，Redis也提供了多种缓存同步策略，主要是在性能和安全中做取舍。</p>
<h4 id="AOF重写操作："><a href="#AOF重写操作：" class="headerlink" title="AOF重写操作："></a>AOF重写操作：</h4><p>主进程执行fork创建子进程。重写操作由子进程执行，父进程继续执行客户端命令。</p>
<p>fork完成时，主进程会将数据同时写到AOF缓冲区和AOF重写缓冲区。fork完成，子进程根据内存快照写入新AOF文件，然后通知主进程，主进程把重写缓冲区数据合并到新AOF文件，替换老的AOF文件。</p>
<img src="https://gitee.com/dtyytop/blogimage/raw/master/img/20210607235748.png" alt="image-20210607235748663" style="zoom: 67%;">

<h3 id="2-3-快问快答"><a href="#2-3-快问快答" class="headerlink" title="2.3 快问快答"></a>2.3 快问快答</h3><h4 id="文件同步策略的底层原理？"><a href="#文件同步策略的底层原理？" class="headerlink" title="文件同步策略的底层原理？"></a>文件同步策略的底层原理？</h4><p>同步策略有always、everysec、no 3种。</p>
<ul>
<li><p>always基于系统fsync操作，针对单个文件做强制硬盘同步，fsync会导致阻塞。安全、性能差。</p>
</li>
<li><p>everysec基于系统write操作，触发延迟写机制，写入系统缓冲区。同步硬盘的操作依赖于系统调度。如果出现系统故障，缓存区的数据可能丢失。提高了性能，不保证数据安全。</p>
</li>
</ul>
<h4 id="重写为什么会使AOF文件变小？"><a href="#重写为什么会使AOF文件变小？" class="headerlink" title="重写为什么会使AOF文件变小？"></a>重写为什么会使AOF文件变小？</h4><ol>
<li>进程中已经超时的数据不再写入</li>
<li>新的AOF文件只包含最终数据的写入指令，丢弃无效命令。</li>
<li>存在写命令合并。</li>
</ol>
<h3 id="2-3-AOF的优劣势"><a href="#2-3-AOF的优劣势" class="headerlink" title="2.3 AOF的优劣势"></a>2.3 AOF的优劣势</h3><ul>
<li>优势<ol>
<li><u>AOF更加健壮，可以秒级备份。</u>增加了可靠性和数据完整性。</li>
<li><u>以日志形式追加</u>，如果磁盘已满，存在未执行完整的写入命令，可以使用redis-check-aof工具修复。</li>
<li><u>当AOF文件过大时，后台可以自动对AOF进行重写。</u>重写的新AOF文件包含恢复当前数据所需的最小命令集合。<strong>重写操作非常安全</strong>，因为Redis 在创建新 AOF 文件的过程中，会继续将命令追加到现有的 AOF 文件里面，即使重写过程中发生停机，现有的 AOF 文件也不会丢失。 而一旦新 AOF 文件创建完毕，Redis 就会从旧 AOF 文件切换到新 AOF 文件，并开始对新 AOF 文件进行追加操作。</li>
<li>AOF日志包含了所有写操作，便于Redis解析恢复。例如执行了flushall/flushdb操作，删除了所有数据，只需要停止Redis服务器，删除AOF末尾的删除命令，再重启Redis即可恢复到flush操作之前的状态。</li>
</ol>
</li>
</ul>
<blockquote>
<p>扩展：RDB模式下，如果误操作删除了所有数据，重启后也会恢复吗？</p>
<p>备份数据也会跟着同步，数据无法恢复。</p>
</blockquote>
<ul>
<li>劣势<ol>
<li>相同的数据，AOF比RDB体积大</li>
<li>某些同步机制下，AOF比RDB慢。秒级备份略低于RDB，每次写入备份，影响性能。</li>
<li>以前AOF发生过bug，就是通过AOF记录的日志，进行数据恢复的时候，没有恢复一模一样的数据出来。不过AOF为了重写过程导致bug，每次重写不是基于旧的指令日志进行merge，而是当前缓存的指令重构，更加健壮。</li>
</ol>
</li>
</ul>
<h2 id="3-RDB和AOF的选择"><a href="#3-RDB和AOF的选择" class="headerlink" title="3 RDB和AOF的选择"></a>3 RDB和AOF的选择</h2><p>RDB会有一段时间的缓存丢失，AOF实时性较好。</p>
<p>通常同时开启RDB和AOF持久化，RDB做冷备，可以在不同时期对不同版本做恢复；AOF做热备，保证数据只有1秒的损失。当AOF破损不可用时，再用RDB恢复。</p>
<p>Redis恢复时，<strong>先加载AOF</strong>，如果没有AOF再加载RDB，达到冷热互备。</p>
<blockquote>
<p>为什么RDB恢复比AOF恢复快？</p>
<p>RDB，是数据文件，恢复时直接加载到内存既可以。而AOF是指令日志，做恢复时，其实是执行所有写操作恢复数据。</p>
</blockquote>
<h2 id="4-持久化阻塞"><a href="#4-持久化阻塞" class="headerlink" title="4 持久化阻塞"></a>4 持久化阻塞</h2><p>Redis持久化阻塞主线程的场景有fork阻塞和AOF追加阻塞。</p>
<h3 id="4-1-fork阻塞"><a href="#4-1-fork阻塞" class="headerlink" title="4.1 fork阻塞"></a>4.1 fork阻塞</h3><p>RDB备份和AOF重写时，进行fork操作。</p>
<p>fork是重量级操作，子线程虽然不需要拷贝主线程物理内存，但是需要拷贝主线程空间内存页列表，fork耗时与总内存量有关。</p>
<p>fork优化操作有</p>
<ol>
<li>降低fork频率，避免不必要的全量复制。</li>
<li>控制Redis实例大小，内存小，fork操作耗时也少。</li>
<li>硬件上优先使用高效的物理机或虚拟化技术，提高fork速度。</li>
</ol>
<h3 id="4-2-AOF追加阻塞"><a href="#4-2-AOF追加阻塞" class="headerlink" title="4.2 AOF追加阻塞"></a>4.2 AOF追加阻塞</h3><p>AOF常用everysec同步策略。</p>
<ol>
<li>主线程负责写入AOF缓冲区。</li>
<li>AOF线程负责每秒执行同步磁盘操作，并记录最近一次同步时间。</li>
<li>主线程对比与上次AOF同步成功时间，如果在2秒内，直接返回；如果大于2秒，则阻塞，直到同步完成。</li>
</ol>
<p>也就是说，everysec最多丢失2秒的数据，如果fsync操作缓慢，会严重影响性能。</p>
<p>优化措施：使用iotop/iostat监控磁盘IO进程，优化系统硬盘负载。</p>
<h2 id="5-多实例部署"><a href="#5-多实例部署" class="headerlink" title="5 多实例部署"></a>5 多实例部署</h2><p>Redis单线程架构导致无法充分利用CPU多核特性，通常是在一台机器部署多个Redis实例。但是多个实例都开启AOF重写后，可能会导致CPU和IO竞争。为了防止这一现象，建议做隔离控制，轮询控制AOF重写。</p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p><a target="_blank" rel="noopener" href="http://www.redis.cn/topics/persistence.html">http://www.redis.cn/topics/persistence.html</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">dtyy</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://dtyytop.gitee.io/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-chi-jiu-hua-ji-zhi/">https://dtyytop.gitee.io/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-chi-jiu-hua-ji-zhi/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://dtyytop.gitee.io" target="_blank">Reality</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Redis/">Redis</a><a class="post-meta__tags" href="/tags/%E9%9D%A2%E8%AF%95/">面试</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222634.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://gitee.com/dtyytop/blogimage/raw/master/img/20210515001034.png" target="_blank"><img class="post-qr-code-img" src="https://gitee.com/dtyytop/blogimage/raw/master/img/20210515001034.png" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="https://gitee.com/dtyytop/blogimage/raw/master/img/20210515001009.png" target="_blank"><img class="post-qr-code-img" src="https://gitee.com/dtyytop/blogimage/raw/master/img/20210515001009.png" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-kuai-wen-kuai-da/"><img class="prev-cover" src="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222600.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Redis快问快答</div></div></a></div><div class="next-post pull-right"><a href="/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-zhu-cong-fu-zhi-yu-yuan-li/"><img class="next-cover" src="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222646.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Redis主从复制架构与原理</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-zhu-cong-fu-zhi-yu-yuan-li/" title="Redis主从复制架构与原理"><img class="cover" src="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222646.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-05-30</div><div class="title">Redis主从复制架构与原理</div></div></a></div><div><a href="/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-kuai-wen-kuai-da/" title="Redis快问快答"><img class="cover" src="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222600.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-05-30</div><div class="title">Redis快问快答</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">dtyy</div><div class="author-info__description">Java全栈</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">95</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/dtyytop"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/dtyytop" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:1607961042@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-RDB%E6%96%B9%E5%BC%8F%E2%80%93Redis-DataBase"><span class="toc-number">1.</span> <span class="toc-text">1 RDB方式–Redis DataBase</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-%E4%BB%80%E4%B9%88%E6%98%AFRDB"><span class="toc-number">1.1.</span> <span class="toc-text">1.1 什么是RDB</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-RDB%E8%87%AA%E5%8A%A8%E8%A7%A6%E5%8F%91%E9%85%8D%E7%BD%AE"><span class="toc-number">1.2.</span> <span class="toc-text">1.2 RDB自动触发配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-RDB%E4%BC%98%E5%8A%A3%E5%8A%BF"><span class="toc-number">1.3.</span> <span class="toc-text">1.3 RDB优劣势</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-RDB%E8%BF%90%E4%BD%9C%E6%B5%81%E7%A8%8B"><span class="toc-number">1.4.</span> <span class="toc-text">1.4 RDB运作流程</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-AOF%E6%96%B9%E5%BC%8F%E2%80%93Append-Only-File"><span class="toc-number">2.</span> <span class="toc-text">2 AOF方式–Append Only File</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-AOF%E7%9A%84%E9%85%8D%E7%BD%AE"><span class="toc-number">2.1.</span> <span class="toc-text">2.1 AOF的配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-AOF%E8%BF%90%E4%BD%9C%E6%B5%81%E7%A8%8B"><span class="toc-number">2.2.</span> <span class="toc-text">2.2 AOF运作流程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#AOF%E7%9A%84%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B%EF%BC%9A"><span class="toc-number">2.2.1.</span> <span class="toc-text">AOF的工作流程：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#AOF%E9%87%8D%E5%86%99%E6%93%8D%E4%BD%9C%EF%BC%9A"><span class="toc-number">2.2.2.</span> <span class="toc-text">AOF重写操作：</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-%E5%BF%AB%E9%97%AE%E5%BF%AB%E7%AD%94"><span class="toc-number">2.3.</span> <span class="toc-text">2.3 快问快答</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E5%90%8C%E6%AD%A5%E7%AD%96%E7%95%A5%E7%9A%84%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86%EF%BC%9F"><span class="toc-number">2.3.1.</span> <span class="toc-text">文件同步策略的底层原理？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E5%86%99%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BC%9A%E4%BD%BFAOF%E6%96%87%E4%BB%B6%E5%8F%98%E5%B0%8F%EF%BC%9F"><span class="toc-number">2.3.2.</span> <span class="toc-text">重写为什么会使AOF文件变小？</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-AOF%E7%9A%84%E4%BC%98%E5%8A%A3%E5%8A%BF"><span class="toc-number">2.4.</span> <span class="toc-text">2.3 AOF的优劣势</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-RDB%E5%92%8CAOF%E7%9A%84%E9%80%89%E6%8B%A9"><span class="toc-number">3.</span> <span class="toc-text">3 RDB和AOF的选择</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E6%8C%81%E4%B9%85%E5%8C%96%E9%98%BB%E5%A1%9E"><span class="toc-number">4.</span> <span class="toc-text">4 持久化阻塞</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-fork%E9%98%BB%E5%A1%9E"><span class="toc-number">4.1.</span> <span class="toc-text">4.1 fork阻塞</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-AOF%E8%BF%BD%E5%8A%A0%E9%98%BB%E5%A1%9E"><span class="toc-number">4.2.</span> <span class="toc-text">4.2 AOF追加阻塞</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E5%A4%9A%E5%AE%9E%E4%BE%8B%E9%83%A8%E7%BD%B2"><span class="toc-number">5.</span> <span class="toc-text">5 多实例部署</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83"><span class="toc-number">6.</span> <span class="toc-text">参考</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/06/02/devnotes/ji-qun-yan-jin/nginx/1.3-nginx-jin-cheng-mo-xing-yu-shi-jian-chu-li-ji-zhi/" title="Nginx进程模型"><img src="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222600.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Nginx进程模型"/></a><div class="content"><a class="title" href="/2021/06/02/devnotes/ji-qun-yan-jin/nginx/1.3-nginx-jin-cheng-mo-xing-yu-shi-jian-chu-li-ji-zhi/" title="Nginx进程模型">Nginx进程模型</a><time datetime="2021-06-01T16:00:00.000Z" title="发表于 2021-06-02 00:00:00">2021-06-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/06/02/devnotes/ji-qun-yan-jin/nginx/1.4-nginx-he-xin-wen-jian-jie-gou-fen-xi/" title="Nginx核心配置结构"><img src="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222629.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Nginx核心配置结构"/></a><div class="content"><a class="title" href="/2021/06/02/devnotes/ji-qun-yan-jin/nginx/1.4-nginx-he-xin-wen-jian-jie-gou-fen-xi/" title="Nginx核心配置结构">Nginx核心配置结构</a><time datetime="2021-06-01T16:00:00.000Z" title="发表于 2021-06-02 00:00:00">2021-06-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/05/30/devnotes/ji-qun-yan-jin/nginx/1.2-wei-shi-me-xuan-ze-nginx/" title="集群、代理与Nginx"><img src="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222629.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="集群、代理与Nginx"/></a><div class="content"><a class="title" href="/2021/05/30/devnotes/ji-qun-yan-jin/nginx/1.2-wei-shi-me-xuan-ze-nginx/" title="集群、代理与Nginx">集群、代理与Nginx</a><time datetime="2021-05-29T16:00:00.000Z" title="发表于 2021-05-30 00:00:00">2021-05-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-zhu-cong-fu-zhi-yu-yuan-li/" title="Redis主从复制架构与原理"><img src="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222646.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Redis主从复制架构与原理"/></a><div class="content"><a class="title" href="/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-zhu-cong-fu-zhi-yu-yuan-li/" title="Redis主从复制架构与原理">Redis主从复制架构与原理</a><time datetime="2021-05-29T16:00:00.000Z" title="发表于 2021-05-30 00:00:00">2021-05-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-chi-jiu-hua-ji-zhi/" title="Redis的两种持久化机制"><img src="https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222634.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Redis的两种持久化机制"/></a><div class="content"><a class="title" href="/2021/05/30/devnotes/ji-qun-yan-jin/redis/redis-chi-jiu-hua-ji-zhi/" title="Redis的两种持久化机制">Redis的两种持久化机制</a><time datetime="2021-05-29T16:00:00.000Z" title="发表于 2021-05-30 00:00:00">2021-05-30</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gitee.com/dtyytop/blogimage/raw/master/img/cover/20210525222634.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By dtyy</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>